import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

class TextFieldRoute extends StatefulWidget {
  const TextFieldRoute({Key? key}) : super(key: key);

  @override
  _TextFieldRouteState createState() => _TextFieldRouteState();
}

class _TextFieldRouteState extends State<TextFieldRoute> {
  FocusNode focusNode1 = FocusNode();
  FocusNode focusNode2 = FocusNode();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('文字输入框'),
      ),
      body: Padding(
        padding: EdgeInsets.all(16),
        child: Column(
          children: [
            TextField(
              autofocus: true,
              focusNode: focusNode1,
              toolbarOptions:
                  ToolbarOptions(selectAll: true, copy: true, paste: true),
              decoration: const InputDecoration(labelText: 'input1'),
            ),
            TextField(
              focusNode: focusNode2,
              decoration: const InputDecoration(labelText: 'input2'),
            ),
            Builder(builder: (ctx) {
              return Column(
                children: [
                  ElevatedButton(
                      onPressed: () {
                        FocusScope.of(context).requestFocus(focusNode2);
                      },
                      child: const Text('移动焦点')),
                  ElevatedButton(
                      onPressed: () {
                        focusNode1.unfocus();
                        focusNode2.unfocus();
                      },
                      child: const Text('隐藏键盘'))
                ],
              );
            }),
            const Text("自定义下划线颜色"),
            const TextField(
              decoration: InputDecoration(
                  // labelText: "输入用户名",
                  prefixIcon: Icon(Icons.person),
                  enabledBorder: UnderlineInputBorder(
                      borderSide: BorderSide(color: Colors.green)),
                  focusedBorder: UnderlineInputBorder(
                      borderSide: BorderSide(color: Colors.green))),
            ),
          ],
        ),
      ),
    );
  }
}
